package edu.zju.gis.dbfg.queryserver.config;

import edu.zju.gis.dbfg.queryserver.tool.RestElasticSearchHelper;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@ConfigurationProperties(prefix = "settings")
@Data
public class CommonSetting {
    private List<String> frontEndRegionList;
    private String sessionMaxAge;
    private Float esMinScore;
    private int cpuCoreSize;
    private int maxTaskPoolSize;
    private int taskQueueCapacity;
    private int keepAliveSeconds;
    private float esFetchFactor;
    private double geomDistTolerance;
    private int esPort;
    private String esName;
    private List<String> esNodes;

    @Bean
    public RestClientBuilder getRestClientBuilder() {
        HttpHost[] httpHosts = new HttpHost[this.esNodes.size()];
        for (int i = 0; i < httpHosts.length; i++) {
            httpHosts[i] = new HttpHost(this.esNodes.get(i), this.esPort);
        }
        return RestClient.builder(httpHosts);
    }

    @Bean
    public RestElasticSearchHelper getRestEsHelper(RestClientBuilder builder) {
        return new RestElasticSearchHelper(builder);
    }

}